package io.renren.modules.generator.service.impl;

import io.renren.common.enums.ApplyStatus;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.generator.entity.UserFriendEntity;
import io.renren.modules.generator.service.UserFriendService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.generator.dao.ApplyFriendDao;
import io.renren.modules.generator.entity.ApplyFriendEntity;
import io.renren.modules.generator.service.ApplyFriendService;


@Service("applyFriendService")
public class ApplyFriendServiceImpl extends ServiceImpl<ApplyFriendDao, ApplyFriendEntity> implements ApplyFriendService {

    @Autowired
    private UserFriendService userFriendService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ApplyFriendEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("friend_id", ShiroUtils.getUserId());
        if (StringUtils.isNotBlank((String) params.get("username"))) {
            wrapper.like("username", params.get("username"));
        }
        IPage<ApplyFriendEntity> page = this.page(
                new Query<ApplyFriendEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void passFriend(Long applyId) {
        ApplyFriendEntity applyFriendEntity = new ApplyFriendEntity();
        UserFriendEntity userFriendEntity = new UserFriendEntity();
        UserFriendEntity userFriendEntity2 = new UserFriendEntity();
        applyFriendEntity.setApplyId(applyId);
        //设置状态为以通过
        applyFriendEntity.setApplyStatus(ApplyStatus.PASS.status);
        //设置通过时间
        applyFriendEntity.setDealTime(new Date());
        baseMapper.updateById(applyFriendEntity);

        //获取发起好友申请用户id
        Long userId = baseMapper.selectById(applyId).getUserId();
        //保存好友关系到好友表中
        userFriendEntity.setUserId(userId);
        userFriendEntity.setFriendId(ShiroUtils.getUserId());
        userFriendService.save(userFriendEntity);

        userFriendEntity2.setUserId(ShiroUtils.getUserId());
        userFriendEntity2.setFriendId(userId);
        userFriendService.save(userFriendEntity2);
    }

    @Override
    public void refuseFriend(Long applyId) {
        ApplyFriendEntity applyFriendEntity = new ApplyFriendEntity();
        applyFriendEntity.setApplyId(applyId);
        //设置状态为以通过
        applyFriendEntity.setApplyStatus(ApplyStatus.REFUSE.status);
        //设置拒绝申请时间
        applyFriendEntity.setDealTime(new Date());
        baseMapper.updateById(applyFriendEntity);

    }

    @Override
    public void addFriend(Long friendId) {
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        ApplyFriendEntity applyFriendEntity = new ApplyFriendEntity();
        //设置申请发起时间
        applyFriendEntity.setApplyTime(new Date());
        //设置申请状态为待验证
        applyFriendEntity.setApplyStatus(ApplyStatus.VERIFY.status);
        //设置申请好友id
        applyFriendEntity.setFriendId(friendId);
        applyFriendEntity.setUserId(userEntity.getUserId());
        applyFriendEntity.setUsername(userEntity.getUsername());
        baseMapper.insert(applyFriendEntity);
    }

}
